#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <pthread.h>

pthread_key_t key;
pthread_once_t once = PTHREAD_ONCE_INIT;

void  thread_init() {
	pthread_key_create(&key, NULL);
}

void* func_1(void* arg) {
	int *ptr;
	int i = 2;
	pthread_setspecific(key, &i);

	sleep(5);
	ptr = pthread_getspecific(key);
	printf("%s: %d\n", (char*)arg, *ptr);
}

void* func_2(void* arg) {
	int *ptr;
	int i = 100;
	pthread_setspecific(key, &i);

	sleep(5);
	ptr = pthread_getspecific(key);
	printf("%s: %d\n", (char*)arg, *ptr);
}

int main() {
	pthread_t tid;
	pthread_once(&once, thread_init);
	pthread_create(&tid, NULL, func_1, "thread 1");
	pthread_create(&tid, NULL, func_2, "thread 2");

	pause();
	return 0;
}